<template>
  <div>
    <div class="tip">ladp密码用于游龙其他功能系统的登陆，设置后，如忘记密码，可到此处重置</div>
    <el-form ref="form" :model="form" :rules="rules" :hide-required-asterisk="true">
      <el-form-item prop="ldapPassword">
        <el-input v-model="form.ldapPassword" type="password" size="small" style="width: 300px" placeholder="请输入ldap密码" />
        <el-button size="small" type="primary" style="margin-left: 20px" @click="setLdapPassword">{{ userInfo.haveLdapPassword ? '重置密码' : '设置密码' }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ucApi } from '@/api/api'
import { validatePassword } from '@/utils/validate.js'

export default {
  data() {
    return {
      form: {
        ldapPassword: ''
      },
      rules: {
        ldapPassword: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { trigger: 'blur', validator: validatePassword }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    setLdapPassword() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          await this.$request(ucApi.updateLdapPassword, { password: this.form.ldapPassword })
          this.$message.success('操作成功')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tip {
  margin: 20px 0;
  font-size: 16px;
  color: #f56c6c;
}
</style>
